

// 5. MongoDB 增删改查 
// 查   find   findOne
// 增   insert({},[])   insertOne  insertMany save()
// 改   update updateOne updateMany
// 删除   deleteOne deleteMany remove


// 增  
db.users.insert({username:"huahua",nickname:"阿花",age:18,password:"123456"})
db.users.save({username:"mingMing",age:28,password:"abc213"})
db.users.insertOne({username:"huazi",word:"daydayup",password:"123123",age:25})
db.users.insertMany([
  {username:"meimei",password:"123123",age:22},
  {username:"zhanpeng",password:"123123",age:20}
])

db.users.insert([
  {username:"拉拉",password:"123123",age:29},
  {username:"小左",password:"123123",age:18}
])

db.users.save([
  {username:"大雷",password:"123123",age:33},
  {username:"老王",password:"123123",age:27}
])

db.users.insert({
  username:"wuhan",
  age:39,
  password:"123qwe"
})


// 改   update updateOne updateMany
// db.users.update(query,data)

db.users.update({
  username:"wuhan"
},{
  $set:{
    password:"000999"
  }
})

db.users.updateOne({
   username:'wuhan'
},{
  $set:{
    phone:110
  }
})

db.users.updateMany({

},{
  $set:{
    salary:'20K'
  }
})

db.users.updateMany({
    username:"wuhan"
},{
  $set:{
    age:50
  }
})

db.users.updateMany({
  username:"wuhan"
},{
  $inc:{
    age:10
  }
})


// 删除   deleteOne deleteMany remove

db.users.deleteOne({
   
})

db.users.deleteMany({
    username:"huahua"
})

db.users.remove({
  username:"wuhan"
})

db.users.remove({})

db.users.drop()

// 查   find   findOne
// db.movies.find(query,filed) 
db.movies.find() 
db.movies.find({},{title:1,year:1}) 
db.movies.find({},{title:1,year:1,_id:0}) 
db.movies.findOne({},{_id:0}) 
db.movies.find({},{_id:0,genres:1,title:1}) 

db.movies.distinct('year')
db.movies.distinct('genres')

db.movies.find({},{title:1,_id:0,'rating.average':1})

db.movies.find({
  'rating.average':{
    $gt:9.4
  }
},{title:1,_id:0,'rating.average':1})

db.movies.find({
  'rating.average':{
    $gte:9.4
  }
},{title:1,_id:0,'rating.average':1})


db.movies.find({
  'rating.average':{
    $lte:9.2
  }
},{title:1,_id:0,'rating.average':1})

db.movies.find({
  'rating.average':{
    $gte:9.2,
    $lt:9.4
  }
},{title:1,_id:0,'rating.average':1})

db.movies.find({
  $or:[
    {
      year:'1994'
    },
    {
      'rating.average':{
        $gte:9.4
      }
    }
  ]
},{
  _id:0,
  title:1,
  year:1,
  'rating.average':1
})

db.movies.find({
  title:new RegExp('大')
},{
  _id:0,
  title:1,
  year:1,
  'rating.average':1
})

db.movies.find({
  year:/^1994$/
},{
  _id:0,
  title:1,
  year:1,
  'rating.average':1
})

db.movies.find({
  title:/闹/
},{
  _id:0,
  title:1,
  year:1,
  'rating.average':1
})

db.movies.find({
  
},{
  _id:0,
  title:1,
  year:1,
  'rating.average':1,
  'directors.name':1
})

db.movies.find({"rating.average":{$gte:9.3},year:"1994"},{year:1,title:1,_id:0,'rating.average':1})

db.movies.find({"rating.average":{$gte:9.3}},{year:1,title:1,_id:0,'rating.average':1})

db.movies.find({year:{$gt:"1990",$lt:"2000"}},{title:1,year:1,_id:0})

db.movies.find({},{year:1,title:1,_id:0}).sort({year:1})   // 1 表示升序

db.movies.find({},{year:1,title:1,_id:0}).sort({year:-1})   // -1 表示降序

db.movies.find({},{year:1,title:1,_id:0,"rating.average":1}).sort({year:-1,"rating.average":-1})

db.movies.find({},{year:1,title:1,_id:0}).limit(10);

db.movies.find({},{year:1,title:1,_id:0}).skip(5).limit(10);

db.movies.find({},{year:1,title:1,_id:0}).count()

db.movies.find({},{year:1,title:1,_id:0}).size();

db.movies.find({},{year:1,title:1,_id:0}).skip(5).size();

db.movies.find({},{year:1,title:1,_id:0}).skip(5).count();

db.movies.find({},{year:1,title:1,_id:0}).sort({year:-1}).limit(1)

db.movies.aggregate({$group:{"_id":"min",min_value:{$min:'$year'}}})

db.movies.aggregate({$group:{"_id":"max",max_value:{$max:'$year'}}})

db.movies.find({$or:[{year:"1994"},{year:"1997"}]},{year:1,title:1,_id:0})

db.movies.find({year:"1994","rating.average":9.6},{year:1,title:1,_id:0})

db.movies.find({year:{$exists:true}},{year:1,title:1,_id:0})

db.movies.find({year111:{$exists:true}},{year111:1,title:1,_id:0})

db.movies.find({genres:{
  $in:['犯罪']
}},{
  title:1,
  genres:1,
  _id:0
})

db.movies.find({genres:{
  $nin:['犯罪','剧情']
}},{
  title:1,
  genres:1,
  _id:0
})

db.movies.findOne({},{_id:0,year:1,title:1})
db.movies.find({},{_id:0,year:1,title:1}).limit(1)